package com.pdsu.xpf.graduation01.config;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

/**
 * @author maomao
 * @date 2022/1/18 17:51
 */
@Configuration
public class PortalCorsFilter extends CorsFilter {
//    private static String ip = "124.70.53.159";
    private static String ip = "localhost";
    private static Logger logger = LoggerFactory.getLogger(PortalCorsFilter.class);
    public PortalCorsFilter() {
        super(configurationSource());
    }
    private static UrlBasedCorsConfigurationSource configurationSource() {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("http://127.0.0.1:888");
        config.addAllowedOrigin("http://192.168.1.2:8080");
        config.addAllowedOrigin("http://"+ip+":8080");
        config.addAllowedHeader("*");
        config.setMaxAge(36000L);
        config.setAllowedMethods(Arrays.asList("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return source;
    }
}
